package com.stylefeng.guns.modular.api.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.api.service.IUserLotteryService;
import com.stylefeng.guns.modular.platform.model.UserLottery;
import com.stylefeng.guns.modular.system.warpper.UserLotteryWarpper;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-07-26 08:46:37
 */
@Controller
@RequestMapping("/userLottery")
public class UserLotteryController extends BaseController {

    private String PREFIX = "/api/userLottery/";

    @Autowired
    private IUserLotteryService userLotteryService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "userLottery.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/userLottery_add")
    public String userLotteryAdd() {
        return PREFIX + "userLottery_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/userLottery_update/{userLotteryId}")
    public String userLotteryUpdate(@PathVariable Integer userLotteryId, Model model) {
        UserLottery userLottery = userLotteryService.selectById(userLotteryId);
        model.addAttribute("item",userLottery);
        LogObjectHolder.me().set(userLottery);
        return PREFIX + "userLottery_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String userName, String appId) {
    	Page<UserLottery> page = new PageFactory<UserLottery>().defaultPage();
    	UserLottery userLottery = new UserLottery();
    	userLottery.setUserName(userName);
    	userLottery.setAppId(appId);
        List<UserLottery> result = userLotteryService.getUserLotterys(page, userLottery);
        page.setRecords((List<UserLottery>) new UserLotteryWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(UserLottery userLottery) {
        userLotteryService.insert(userLottery);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer userLotteryId) {
        userLotteryService.deleteById(userLotteryId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(UserLottery userLottery) {
        userLotteryService.updateById(userLottery);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{userLotteryId}")
    @ResponseBody
    public Object detail(@PathVariable("userLotteryId") Integer userLotteryId) {
        return userLotteryService.selectById(userLotteryId);
    }
}
